/*
 * Copyright (c) 2009-2013 EvTech Project
 * All rights reserved.
 * 
 * This file is part of Symbol Matching.
 *
 * Symbol Matching is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or 
 * (at your option) any later version.
 *
 * Symbol Matching is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with Symbol Matching.  If not, see <http://www.gnu.org/licenses/>.
 * 
 */
package fi.honkalampisaatio.reaktiopeli.xml;


public class GameTime implements Comparable<GameTime> {
	private long time;
	private int score;
	
	public void setTime(long time) {
		this.time = time;
	}
	public long getTime() {
		return time;
	}
	
	public void setScore(int score) {
		this.score = score;
	}
	public int getScore() {
		return score;
	}
	
	/**
	 * Format have next special characters
	 * h = hours, m = minutes, s = seconds, S = milliseconds
	 * if there are more than one character like hh, it shows zeros also (eg 05)
	 * all between characters ' are literal
	 *  
	 * @param format eg. "hh.mm.ss S 'ms'"
	 * @return time in the given format
	 */
	public String getTime(String format)
	{
		String retString = "";
		String milliseconds = Integer.toString((int)(time) - (int)(time / 1000)*1000); 
		String seconds = Integer.toString((int)(time / 1000) - (int)(time / 60000)*60);  
		String minutes = Integer.toString((int)((time/ 60000)-(int)(time / 3600000)*60));  
		String hours = Integer.toString((int)(time / 3600000));  
		boolean doConvert = true;
		
		for(int i=0; format!=null && i<format.length(); i++)
		{
			if(!doConvert)
			{
				if(format.charAt(i) == '\'')
				{
					doConvert = true;
				}
				else
				{
					retString += format.charAt(i);
				}
				continue;
			}
			switch(format.charAt(i))
			{
			case '\'':
				doConvert = false;
				break;
			case 'h':
				if(i+1<format.length() && format.charAt(i+1)=='h')
				{
					i++;
					if(hours.length()<2)
						retString += '0';
					retString += hours;
				}
				else
				{
					retString += hours;
				}
				break;
			case 'm':
				if(i+1<format.length() && format.charAt(i+1)=='m')
				{
					i++;
					if(minutes.length()<2)
						retString += '0';
					retString += minutes;
				}
				else
				{
					retString += minutes;
				}
				break;
			case 's':
				if(i+1<format.length() && format.charAt(i+1)=='s')
				{
					i++;
					if(seconds.length()<2)
						retString += '0';
					retString += seconds;
				}
				else
				{
					retString += seconds;
				}
				break;
			case 'S':
				if(i+1<format.length() && format.charAt(i+1)=='S')
				{
					i++;
					if(milliseconds.length()<2)
						retString += '0';
					retString += milliseconds;
				}
				else
				{
					retString += milliseconds;
				}
				break;
			default:
				retString += format.charAt(i);
				break;
			}
		}
		return retString;
	}
	
	@Override
	public int compareTo(GameTime o) {
		if(o==null || this.score < o.score)
		{
			return 1;	
		}
		else if(this.score > o.score)
		{
			return -1;
		}
		else
		{
			if(this.score == 0 && o.score == 0)
			{
				return 0;
			}
			else if((this.time/this.score) < (o.time/o.score))
			{
				return -1;
			}
			else if((this.time/this.score) > (o.time/o.score))
			{
				return 1;
			}
			else
			{
				return 0;
			}	
		}
	}
}
